#include <QtGui/QApplication>
#include <QDateTime>
#include "MainWindow.h"
#include "Core.h"

void myMessageHandler(QtMsgType type, const char *msg)
{
QString txt;
QDateTime date;

QFile outFile(QDesktopServices::storageLocation(QDesktopServices::DataLocation) + "/log.txt");
    if(outFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Unbuffered | QIODevice::Append))
    {
        switch (type) {
             case QtDebugMsg:
                {
                 txt.append("Debug: ").append(msg);
                 break;
                }
             case QtWarningMsg:
                {
                 txt.append("Warning: ").append(msg);
                 break;
                }
             case QtCriticalMsg:
                {
                 txt.append("Critical: ").append(msg);
                 break;
                }
            default:
                break;
        }
        txt.prepend(" - ");
        txt.prepend(date.currentDateTime().toString());
        txt.append("\n");
        outFile.write(txt.toUtf8());
        outFile.close();

    }
}

int main(int argc, char *argv[])
{
   // qInstallMsgHandler(myMessageHandler);
    QApplication a(argc, argv);

    if (!QSystemTrayIcon::isSystemTrayAvailable()) {
             QMessageBox::critical(0, QObject::tr("Systray"),
                                   QObject::tr("I couldn't detect any system tray "
                                               "on this system."));
             return 1;
         }

    QCoreApplication::setOrganizationName("RT");
    QCoreApplication::setApplicationName("Mail");

    MainWindow w;
    Core c;
    a.connect(&w,SIGNAL(CreateDatabase(QString)),&c,SLOT(CreateDatabase(QString)));
    a.connect(&w,SIGNAL(DeleteDatabase(QString)),&c,SLOT(DeleteDatabase(QString)));
    w.show();
    w.CheckAccount();


    c.CheckDatabase();

    return a.exec();

}




